/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Main;

import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 *
 * @author alwynster
 */
public class FileManager {
    
    static FileConnection settingsConn;
    static RecordStore rs = null;
    
    public static int OpenSettingsFile()
    {
        try
        {
            byte[] name = null;
            boolean exist = false;
            boolean OK = true;
            
            // exist = existing("PowerPong");
            rs = RecordStore.openRecordStore("PowerPong", true);
            if(rs.getNumRecords() == 0)
            {
                 rs.addRecord("0;0".getBytes(), 0, "0;0".length());
                 return 1;
            }
                
            return -1;
//            String private_path = System.getProperty("fileconn.dir.private");
//            if(settingsConn != null)
//                settingsConn.close();
//            if(!private_path.endsWith("/"))
//                private_path += "/";
//
//            settingsConn = (FileConnection) Connector.open(private_path + "config.txt", Connector.READ_WRITE);
//            if(!settingsConn.exists())
//            {
//               settingsConn.create();
//               return 0;
//            }
//            else
//                return 1;
        }
        catch(Exception e)
        {
            return 0;
        }
    }
    
    public boolean existing(String recordStoreName)
    {
        boolean ex = false;
        if(recordStoreName.length() > 32)
            return false;
//        try
        {
//            rs = RecordStore.openRecordStore(recordStoreName, false);
        }
        return true;
    }
    
    public static String ReadSettings(int record)
    {
        try {
            byte[] b = rs.getRecord(record);
            return new String(b);
        } catch (Exception ex) {
            return null;
        }
    }
    
    public static int CloseSettingsFile()
    {
        try
        {
            rs.closeRecordStore();
//            if(settingsConn != null)
//            {
//                settingsConn.close();
//            }
            return 1;
        }
        catch(Exception e)
        {
            return 0;
        }
    }
    
    public static int WriteSettings(String newLine, int record)
    {
//        OutputStream out;
        try
        {
            rs.setRecord(record, newLine.getBytes(), 0, newLine.length());
//            if(!settingsConn.canWrite())
//                return 0;
            
//            out = settingsConn.openOutputStream();
//            out.write(newLine.getBytes());
//            out.flush();
//            out.close();
            return 1;
        }
        catch(Exception e)
        {
            return 0;
        }
    }
    
    public static String ReadSettingsLine()
    {
        byte[] bytes = new byte[1024];
        int length = 0;
        
        try
        {
            InputStream in = settingsConn.openInputStream();
            
            in.read(bytes);
            in.close();
            
            for(int x = 0 ; x < bytes.length; x++)
            {
                if(bytes[x] == 0 || bytes[x] == '\n')
                {
                    length = x;
                    break;
                }
            }
            
            return new String(bytes, 0, length);
        }
        catch(Exception e)
        {
            return null;
        }
    }
    
    public static int GetSize() {
        try {
            return rs.getNumRecords();
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
            return -1;
        }
 }
}
